#include "mainscene.h"
#include "ui_mainscene.h"
#include<QPushButton>
#include<QDebug>
//#include<QAction>
#include<QPainter>
#include<QPixmap>
#include"mypushbutton.h"
#include<QTimer>
#include"chooselevelscene.h"
#include<QSound>

MainScene::MainScene(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainScene)
{
    ui->setupUi(this);
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":res/Coin0001.png"));
    this->setWindowTitle("翻金币案例");

    //点击退出，退出游戏
    connect(ui->actionQuit,&QAction::triggered,[=](){
        this->close();
    });

    //准备开始按钮的音效
    QSound *StartSound = new QSound(":/res/TapButtonSound.wav",this);


    //创建开始按钮
    MyPushButton * startBtn = new MyPushButton(":/res/MenuSceneStartButton.png");
    startBtn->setParent(this);
    startBtn->move(this->width()*0.5-startBtn->width()*0.5,this->height()*0.7);
    //实例化选择关卡的场景
    chooseScene = new ChooseLevelScene;
    //监听点击事件,执行特效
    connect(startBtn,&QPushButton::clicked,[=](){
        //播放音效资源
        StartSound->play();

        //弹跳
        startBtn->zoom1();
        startBtn->zoom2();
        //延时0.5秒，选择进入场景
        QTimer::singleShot(500,this,[=](){
            chooseScene->setGeometry(this->geometry());
            this->hide();
            chooseScene->show();

        });
    });
    //监听选择关卡的返回信号的按钮
    connect(chooseScene,&ChooseLevelScene::chooseSceneBack,[=](){

        QTimer::singleShot(500,this,[=](){
            //设置chooseScene场景的位置
            this->setGeometry(chooseScene->geometry());
            chooseScene->hide();
            this->show();

        });
    });



}

MainScene::~MainScene()
{
    delete ui;
}
void MainScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    //绘制背景图


    //加载标题
    pix.load(":/res/Title.png");
    //qDebug()<<"缩放前：宽"<<pix.width()<<"高"<<pix.height();
    //缩放图片
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    //qDebug()<<"缩放后：宽"<<pix.width()<<"高"<<pix.height();
    //绘制标题
    painter.drawPixmap(10,30,pix.width(),pix.height(),pix);


}

